package com.qys.livesMall.system.controller.sys;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qys.livesMall.common.result.R;
import com.qys.livesMall.system.modal.dto.BankDTO;
import com.qys.livesMall.system.modal.dto.QueryBankDTO;
import com.qys.livesMall.system.modal.entity.Bank;
import com.qys.livesMall.system.modal.vo.BankVO;
import com.qys.livesMall.system.service.BankService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;

import java.io.Serializable;
import java.util.List;

/**
 * 银行表(Bank)表控制层
 *
 * @author makejava
 * @since 2023-07-07 13:50:10
 */
@RestController
@RequestMapping("/sys/bank")
@RequiredArgsConstructor
@Tag(name = "sys-银行管理", description = "sys-银行管理")
public class SysBankController {

    private final BankService bankService;

    /**
     * 分页查询所有数据
     *
     * @return 所有数据
     */
    @PostMapping("/selectAll")
    @Operation(summary = "分页查询所有数据")
    public R<IPage<BankVO>> selectAll(@RequestBody QueryBankDTO queryBankDTO) {
        IPage<BankVO> convert = bankService.page(new Page<>(queryBankDTO.getPageNum(), queryBankDTO.getPageSize()), Wrappers.query()).convert(e -> {
            BankVO bankVO = new BankVO();
            BeanUtils.copyProperties(e, bankVO);
            return bankVO;
        });
        return R.ok(convert);
    }

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("/{id}")
    @Operation(summary = "通过主键查询单条数据")
    public R<BankVO> selectOne(@PathVariable Serializable id) {
        Bank bank = bankService.getById(id);
        if (null == bank) {
            return R.ok();
        }
        BankVO bankVO = new BankVO();
        BeanUtils.copyProperties(bank, bankVO);
        return R.ok(bankVO);
    }

    /**
     * 新增/修改数据
     *
     * @param bankDTO 实体对象
     * @return 新增结果
     */
    @PostMapping("/edit")
    @Operation(summary = "新增/修改数据")
    public R<?> edit(@RequestBody BankDTO bankDTO) {
        Bank bank = new Bank();
        BeanUtils.copyProperties(bankDTO, bank);
        return R.ok(bankService.saveOrUpdate(bank));
    }

    /**
     * 修改状态
     *
     * @return 新增结果
     */
    @GetMapping("/changeStatus/{id}")
    @Operation(summary = "修改状态")
    public R<?> changeStatus(@PathVariable Serializable id) {
        Bank bank = bankService.getById(id);
        if (null == bank) {
            R.fail();
        }
        Bank newBank = new Bank();
        newBank.setId(bank.getId());
        newBank.setState(bank.getState() == 1 ? 0 : 1);
        bankService.updateById(newBank);
        return R.ok();
    }

    /**
     * 删除数据
     *
     * @param idList 主键结合
     * @return 删除结果
     */
    @PostMapping("/delete")
    @Operation(summary = "删除数据")
    public R<?> delete(@RequestBody List<Long> idList) {
        try {
            bankService.removeBank(idList);
        } catch (Exception e) {
            return R.fail(e.getMessage());
        }
        return R.ok();
    }
}

